/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufba.lasid.v1.tests;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.RunInstancesRequest;
import com.amazonaws.services.ec2.model.RunInstancesResult;
import static java.lang.System.out;
import java.util.List;


/**
 *
 * @author aliriosa
 */
public class TestAWS {
 
    public static void main(String[] args){
        
        String accessKey = "WKy3rMzOWPouVOxK1p3Ar1C2uRBwa2FBXnCw";//args[0];
        String secretKey = "orZbksd23lYWirnzNBfGR07NE9566zKgjILgw"; //args[1];
        String clcEndpoint = "http://192.168.188.100:8773/services/Eucalyptus"; //args[2];
        
        
        //System.getProperties().put("HTTPClient.dontChunkRequests", "false");
        
        BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
        
        ClientConfiguration cc = new ClientConfiguration();
        
        AmazonEC2Client ec2 = new AmazonEC2Client(credentials);                
             
        ec2.setEndpoint(clcEndpoint);
                        
//        DescribeAvailabilityZonesResult result = ec2.describeAvailabilityZones();
//        
//        for(AvailabilityZone zone : result.getAvailabilityZones()){
//            out.println(zone.getZoneName());            
//            
//        }
        
//       DescribeReservedInstancesResult rir = ec2.describeReservedInstances();
//       
//       if(rir == null){
//           out.println("Describe Reserved Instances returns null");
//       }
//       for(ReservedInstances ri : rir.getReservedInstances()){
//           out.println("instance type: " + ri.getInstanceType() + "; max = " + ri.getInstanceCount());
//       }                                               
//       
       
        RunInstancesRequest instancesReq = new RunInstancesRequest()
            .withInstanceType("m1.small ")
            .withImageId("emi-427C125A")
            .withMinCount(1)
            .withMaxCount(1)
            .withSecurityGroupIds("admin")
            .withKeyName("mykey4")
           // .withUserData(Base64.encodeBase64String(myUserData.getBytes()))        
           ; 
        
        RunInstancesResult instancesRep = ec2.runInstances(instancesReq);
        
        List<Instance> instances = instancesRep.getReservation().getInstances();
        for (Instance instance : instances) {
            
            out.println("instance ID: " + instance.getInstanceId());
            
        }        
        
        
    }
    
}
